﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Questionary.Model;
using Microsoft.Office.Interop.ExcelEdit;
using System.Threading;

namespace Questionary.Logic
{
    public class CountToExcel
    {

        public static bool Census(string currentPath)
        {
            try
            {
                //统计部分
                TotalCountData tcd = Count(currentPath);
                //写入excel部分
                WriteToExcel(tcd);
            }
            catch (Exception ex)
            {
                return false;
            }
            return false;
        }

        private static void WriteToExcel(TotalCountData tcd)
        {
            try
            {
                ExcelHelper excelHelper = new ExcelHelper();
                excelHelper.KillProcess("EXCEL.exe");
                Thread.Sleep(2000);//等待kill完成;
                //如果存在该文件先删除
                if (File.Exists(HttpContext.Current.Request.MapPath(FilePaths.GeneralExcelFilePath)))
                {
                    File.Delete(HttpContext.Current.Request.MapPath(FilePaths.GeneralExcelFilePath));
                }

                excelHelper.Create();
                
                //创建新的excel文件
                if (!File.Exists(HttpContext.Current.Request.MapPath(FilePaths.GeneralExcelFilePath)))
                {
                    FileStream fs = File.Create(HttpContext.Current.Request.MapPath(FilePaths.GeneralExcelFilePath));
                    fs.Close();
                }
                File.SetAttributes(HttpContext.Current.Request.MapPath(FilePaths.GeneralExcelFilePath), FileAttributes.Normal);
                excelHelper.Open(HttpContext.Current.Request.MapPath(FilePaths.GeneralExcelFilePath));
                excelHelper.AddSheet("Sheet1");
               
                Microsoft.Office.Interop.Excel.Worksheet ws = excelHelper.GetSheet("Sheet1");
                //写入选择学校数目
                excelHelper.UniteCells(ws, 1, 1, 1, 2);
                excelHelper.SetCellValue(ws, 1, 1, "各校参加调查的人数统计");
                  

                Dictionary<string, int> schoolCount = tcd.SchoolCount;
                int k = 2;
                foreach (string schoolName in schoolCount.Keys)
                {               
                    excelHelper.SetCellValue(ws, k, 1, schoolName);//写入学校名称
                    excelHelper.SetCellValue(ws, k, 2, schoolCount[schoolName]);//写入选择学校数量
                    k++;
                }
                
                //写入选择题部分
                excelHelper.SetCellValue(ws, 1, 4, "题目");
                excelHelper.SetCellValue(ws, 1, 5, "A");
                excelHelper.SetCellValue(ws, 1, 6, "B");
                excelHelper.SetCellValue(ws, 1, 7, "C");
                excelHelper.SetCellValue(ws, 1, 8, "D");
                excelHelper.SetCellValue(ws, 1, 9, "E");
                excelHelper.SetCellValue(ws, 1, 10, "F");



                for (int i = 0; i < tcd.SelectionCount.Keys.Count; i++)
                {
                    excelHelper.SetCellValue(ws, i + 2, 4, i + 1);
                    for (int j = 0; j < 6; j++)
                    {
                        excelHelper.SetCellValue(ws, i + 2, j + 5, tcd.SelectionCount[i].OptionCount[j]);
                    }
                }

                int h = 0;           
                //写入主观题部分
                foreach (int key in tcd.Content.Keys)
                {                  
                    int j = 0;
                    excelHelper.SetCellValue(ws, 1, h + 12, "第" + (key+1) + "题");
                    foreach (string content in tcd.Content[key])
                    {
                        excelHelper.SetCellValue(ws, j + 2, h + 12, content);
                        j++;
                    }
                    h++;
                }
                excelHelper.Save();                
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + e.StackTrace);
            }
        }

        private static TotalCountData Count(string currentPath)
        {
            TotalCountData tcd = new TotalCountData();
            try
            {             
                #region  统计部分
                //遍历文件夹
                DirectoryInfo TheFolder = new DirectoryInfo(currentPath);
                List<ActiveItem> activeList = new List<ActiveItem>();
                foreach (FileInfo NextFile in TheFolder.GetFiles())
                {
                    //遍历序列化对象
                    ActiveItem ai = FileHelper.GetObject<ActiveItem>(currentPath + "\\" + NextFile.Name);
                    activeList.Add(ai);
                }
                //统计数量
                foreach (ActiveItem ai in activeList)
                {
                    //选择学校数目
                    string schoolName = ai.SchoolName;
                    if (tcd.SchoolCount.ContainsKey(schoolName))
                    {
                        tcd.SchoolCount[schoolName]++;
                    }
                    else
                    {
                        tcd.SchoolCount[schoolName] = 1;
                    }
                    List<AnswerItem> answerItem = ai.Answers;
                    for (int i = 0; i < answerItem.Count; i++)
                    {
                        AnswerItem anwser = answerItem[i];
                        //选择题   
                        List<int> siList = anwser.Selections;
                        for (int j = 0; j < siList.Count; j++)
                        {
                            int select = siList[j];
                            if (tcd.SelectionCount.ContainsKey(i))
                            {
                                tcd.SelectionCount[i].OptionCount[select]++;
                            }
                            else
                            {
                                tcd.SelectionCount.Add(i, new SelectionCount());
                                tcd.SelectionCount[i].OptionCount[select] = 1;
                            }
                        }
                        //主观题
                        string content = anwser.Content;
                        if (content != null)
                        {
                            if (!tcd.Content.ContainsKey(i))
                            {
                                tcd.Content.Add(i, new List<string>());
                            }                         
                            tcd.Content[i].Add(content);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + ex.StackTrace);
            }
            return tcd;
            #endregion
        }


    }
}